Skip to content

Enable jinja loop controls (break/continue)#88625

Merged
balloob merged 1 commit into
home-assistant:devfrom
depoll:jinja-loop-controls
Feb 24, 2023
Merged

Enable jinja loop controls (break/continue)#88625
balloob merged 1 commit into
home-assistant:devfrom
depoll:jinja-loop-controls

Conversation

@depoll
Copy link
Copy Markdown
Contributor

@depoll depoll commented Feb 22, 2023

Breaking change

Proposed change

Breaking out #88575

Turns on break and continue for for loops, which allows short-circuiting of loops to improve efficiency of templates and clarity of code.

All of this enables the following code:

{%- for v in range(10) %}
    {%- if v == 1 -%}
        {%- continue -%}
    {%- elif v == 3 -%}
        {%- break -%}
    {%- endif -%}
    {{ v }}
{%- endfor -%}

To produce the following output:

02

I'm happy to update the documentation as well but wanted to first clear that this would be a welcome change.

Type of change

  • Dependency upgrade
  • Bugfix (non-breaking change which fixes an issue)
  • New integration (thank you!)
  • New feature (which adds functionality to an existing integration)
  • Deprecation (breaking change to happen in the future)
  • Breaking change (fix/feature causing existing functionality to break)
  • Code quality improvements to existing code or addition of tests

Additional information

  • This PR fixes or closes issue: fixes #
  • This PR is related to issue:
  • Link to documentation pull request:

Checklist

  • The code change is tested and works locally.
  • Local tests pass. Your PR cannot be merged unless tests pass
  • There is no commented out code in this PR.
  • I have followed the development checklist
  • The code has been formatted using Black (black --fast homeassistant tests)
  • Tests have been added to verify that the new code works.

If user exposed functionality or configuration variables are added/changed:

If the code communicates with devices, web services, or third-party tools:

  • The manifest file has all fields filled out correctly.
    Updated and included derived files by running: python3 -m script.hassfest.
  • New or updated dependencies have been added to requirements_all.txt.
    Updated by running python3 -m script.gen_requirements_all.
  • For the updated dependencies - a link to the changelog, or at minimum a diff between library versions is added to the PR description.
  • Untested files have been added to .coveragerc.

To help with the load of incoming pull requests:

@depoll depoll requested a review from a team as a code owner February 22, 2023 18:41
@home-assistant home-assistant Bot added cla-signed core new-feature small-pr PRs with less than 30 lines. labels Feb 22, 2023
@depoll depoll mentioned this pull request Feb 22, 2023
19 tasks
@depoll depoll changed the title Enables jinja loop controls (break/continue) Enable jinja loop controls (break/continue) Feb 22, 2023
@balloob balloob merged commit af49b98 into home-assistant:dev Feb 24, 2023
@github-actions github-actions Bot locked and limited conversation to collaborators Feb 25, 2023
@frenck frenck added the noteworthy Marks a PR as noteworthy and should be in the release notes (in case it normally would not appear) label Feb 27, 2023
@home-assistant home-assistant unlocked this conversation Feb 28, 2023
@emontnemery
Copy link
Copy Markdown
Contributor

emontnemery commented Feb 28, 2023

@depoll this is really nice!
However, as you point out in the PR description we need to document it.
Maybe add a new section right before https://www.home-assistant.io/docs/configuration/templating/#home-assistant-template-extensions named "Enabled Jinja extensions" or something like that?

@depoll
Copy link
Copy Markdown
Contributor Author

depoll commented Feb 28, 2023

Thanks! Will try to get to the docs on Wednesday.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

cla-signed core new-feature noteworthy Marks a PR as noteworthy and should be in the release notes (in case it normally would not appear) small-pr PRs with less than 30 lines.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants